﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Entity;
using System.Data.Sql;
using System.Data.SqlClient;
namespace DAL
{
    public class ModuleDao
    {
        DbCommon.DbHelper DbHelper = new DbCommon.DbHelper();
        public List<BoModules> GetAllModules()
        {
            List<BoModules> modules = new List<BoModules>();
            try
            {
                DataTable dt = new DataTable();
                string sqlstr = "select MOD_ID, MOD_NAME, MOD_DISPLAYNAME, MOD_PARENTID, MOD_URL from Modules where MOD_IsShow=1 order by MOD_ID ";
                dt = DbHelper.ExcuteQuery(sqlstr);
                
                foreach (DataRow dr in dt.Rows)
                {
                    BoModules mod = new BoModules();
                    mod.MOD_ID = dr["MOD_ID"].ToString();
                    mod.MOD_NAME = dr["MOD_NAME"].ToString();
                    mod.MOD_DISPLAYNAME = dr["MOD_DISPLAYNAME"].ToString();
                    mod.MOD_DISPLAYNAME = (dr.IsNull("MOD_PARENTID"))? null: dr["MOD_PARENTID"].ToString();
                    mod.MOD_DISPLAYNAME = dr["MOD_URL"].ToString();
                    modules.Add(mod);
                }
                return modules;
            }
            catch (SqlException e)
            {
                return modules;
            }
        }

        public List<BoModules> GetChildModules(string parentId)
        {
            List<BoModules> modules = new List<BoModules>();
            try
            {
                DataTable dt = new DataTable();

                string sqlstr = "select MOD_ID, MOD_NAME, MOD_DISPLAYNAME, MOD_PARENTID, MOD_URL from Modules ";
                if (parentId != null)
                {
                    sqlstr += " where MOD_PARENTID=@ParentId and MOD_IsShow=1 order by MOD_ID ";
                    SqlParameter param = new SqlParameter("@ParentId", SqlDbType.NVarChar, 50);
                    param.Value = parentId;
                    SqlParameter[] pr = new SqlParameter[]{
                        param
                    };
                    dt = DbHelper.ExcuteQuery(sqlstr, pr);
                }
                else
                {
                    sqlstr += " where MOD_PARENTID IS NULL and MOD_IsShow=1 order by MOD_ID ";
                    dt = DbHelper.ExcuteQuery(sqlstr);
                }

                foreach (DataRow dr in dt.Rows)
                {
                    BoModules mod = new BoModules();
                    mod.MOD_ID = dr["MOD_ID"].ToString();
                    mod.MOD_NAME = dr["MOD_NAME"].ToString();
                    mod.MOD_DISPLAYNAME = dr["MOD_DISPLAYNAME"].ToString();
                    mod.MOD_PARENTID = (dr.IsNull("MOD_PARENTID")) ? null : dr["MOD_PARENTID"].ToString();
                    mod.MOD_URL = dr["MOD_URL"].ToString();
                    modules.Add(mod);
                }
                return modules;
            }
            catch (SqlException e)
            {
                return modules;
            }
        }

        public List<BoModules> GetRoleModules(string roleId)
        {
            List<BoModules> modules = new List<BoModules>();
            try
            {
                DataTable dt = new DataTable();

                string sqlstr = "select MOD_ID, MOD_NAME, MOD_DISPLAYNAME, MOD_PARENTID, MOD_URL ";
                sqlstr += " from Modules AS M inner join Privileges AS P on M.MOD_ID=P.PRI_MODULES ";
                sqlstr += " where P.PRI_ROLES=@RoleId and M.MOD_IsShow=1 order by M.MOD_ID ";
                
                SqlParameter[] pr = new SqlParameter[]{
                    new SqlParameter("@RoleId", roleId)
                };
                dt = DbHelper.ExcuteQuery(sqlstr, pr);
                

                foreach (DataRow dr in dt.Rows)
                {
                    BoModules mod = new BoModules();
                    mod.MOD_ID = dr["MOD_ID"].ToString();
                    mod.MOD_NAME = dr["MOD_NAME"].ToString();
                    mod.MOD_DISPLAYNAME = dr["MOD_DISPLAYNAME"].ToString();
                    mod.MOD_PARENTID = (dr.IsNull("MOD_PARENTID")) ? null : dr["MOD_PARENTID"].ToString();
                    mod.MOD_URL = dr["MOD_URL"].ToString();
                    modules.Add(mod);
                }
                return modules;
            }
            catch (SqlException e)
            {
                return modules;
            }
        }

        public List<BoModules> GetRoleModules(string roleId, string parentModuleId)
        {
            List<BoModules> modules = new List<BoModules>();
            try
            {
                DataTable dt = new DataTable();

                string sqlstr = "select MOD_ID, MOD_NAME, MOD_DISPLAYNAME, MOD_PARENTID, MOD_URL ";
                sqlstr += " from Modules AS M inner join Privileges AS P on M.MOD_ID=P.PRI_MODULES ";
                sqlstr += " where P.PRI_ROLES=@RoleId and M.MOD_IsShow=1 ";
                if (parentModuleId != null)
                {
                    sqlstr += " and MOD_PARENTID=@ParentId order by M.MOD_ID";
                    
                    SqlParameter[] pr = new SqlParameter[]{
                        new SqlParameter("@ParentId", parentModuleId),
                        new SqlParameter("@RoleId", roleId)
                    };
                    dt = DbHelper.ExcuteQuery(sqlstr, pr);
                }
                else
                {
                    sqlstr += " and MOD_PARENTID IS NULL order by M.MOD_ID";
                    SqlParameter[] pr = new SqlParameter[]{
                        new SqlParameter("@RoleId", roleId)
                    };
                    dt = DbHelper.ExcuteQuery(sqlstr, pr);
                }

                foreach (DataRow dr in dt.Rows)
                {
                    BoModules mod = new BoModules();
                    mod.MOD_ID = dr["MOD_ID"].ToString();
                    mod.MOD_NAME = dr["MOD_NAME"].ToString();
                    mod.MOD_DISPLAYNAME = dr["MOD_DISPLAYNAME"].ToString();
                    mod.MOD_PARENTID = (dr.IsNull("MOD_PARENTID")) ? null : dr["MOD_PARENTID"].ToString();
                    mod.MOD_URL = dr["MOD_URL"].ToString();
                    modules.Add(mod);
                }
                return modules;
            }
            catch (SqlException e)
            {
                return modules;
            }
        }
    }
}
